<?php

//2008.7.3 dt
function smarty_function_dateCalenderNew($params, &$smarty)
{
	$status = $params["state"];
	$currentDateToyyyymm = $params["currentDateToyyyymm"];
	$_dayRow = array("",31,28,31,30,31,30,31,31,30,31,30,31);
	$_weekRow = array("jp"=>array("日","月","火","水","木","金","土"),
						     "zh"=>array("日","一","二","三","四","五","六"),
	    				     "en"=>array("SUN","MON","TUE","WEN","SUR","FRI","SAT"));
	$datetype = array(
			1 => 'シ1',
			2 => 'シ2',
			3 => 'シ3',
			4 => 'シ4',
			5 => 'シ5',
			6 => 'シ6',
			7 => 'シ7');
	
	$base=new Module_base();
	$baseArr=$base->getAllData();
	
	$calender = new Module_calender();
	
	$_strYear = "";
	$_strWeek = "";
	$_strDay = "";
	$_language = "jp";
	$_Year;
	$_Month;
	$_Day;

	//初始化 begin
	if ($currentDateToyyyymm == "")
	{
		$_Year = date("Y");
		$_Month = date("n");
	}
	else
	{
		$_Year  = substr($currentDateToyyyymm,0,4);//date("Y",strtotime($currentDateToyyyymm));
		if(strlen($currentDateToyyyymm) == 7)
		{
			$_Month = substr($currentDateToyyyymm,5,2);//date("n",strtotime($currentDateToyyyymm));
		}
		else
		{
			$_Month = substr($currentDateToyyyymm,5,1);
		}
	}
	
	if ($Day == "") 
	{
		$_Day = date("d");
	}
	else 
	{
		$_Day = $Day;
	}
	
	//初始化 end
	
	//计算闰年的天数
	if ((($_Year%4 == 0) && ($_Year%100!= 0)) || ($_Year%400==0)) $_dayRow[2]= 29;
	
	//显示年份选择 begin
	if($_language == "en")
	{
		$YYYYMM = $_Year."/".$_Month;
	}
	else
	{
		$YYYYMM = $_Year."年".$_Month."月";
	}

	$_strYear .= "<tr>\r\n"; 
    $_strYear .= "<td colspan='16' align='left' bgcolor='#FFFFFF' class='text02'><strong>".$YYYYMM."</strong></td>\r\n";
    $_strYear .= "</tr>\r\n";
    //显示年份选择 end
    
    //显示星期行 begin
    $_strWeek .= "<tr>\r\n";  
	for($i=1; $i <= 16; $i++) 
	{
		
		if($i == 16)
		{
			$_strWeek .= "<td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td>\r\n";
		}
		else
		{
			$_strWeek .= "<td align='center' valign='middle' bgcolor='#DDE3EC' class='text02'>".$i."</td>\r\n"; 
		}
	}
	$_strWeek .= "</tr>\r\n";
	$_strWeek .= "<tr>\r\n";
	for($j=1;$j <=16;$j++)
	{
		$currentDateByWeek = date("w",strtotime($currentDateToyyyymm."-".$j));
		if($j == 16)
		{
			$_strWeek .="<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;&nbsp;</td>\r\n";
		}
		else
		{
			if($currentDateByWeek == 6)
			{
				$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02 style6'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
			}
			elseif($currentDateByWeek == 0)
			{
				$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02 style3'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
			}
			else
			{
				$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
			}
		}	
	}
	$_strWeek .= "</tr>\r\n";
	$_strWeek .= "<tr>\r\n";
	for($j=1;$j <=16;$j++)
	{
		if($j == 16)
		{
			$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02 style6' width='45'>&nbsp;</td>\r\n";
		}
		else
		{
			$re_date = date("Ymd",strtotime($_Year."-".$_Month."-".$j));
			$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02'><select name='select".$j."' style='width:50px; '>\r\n";
			$pricerank = $calender->getPricerank($re_date);
			
			
			for($i=1;$i <= $baseArr["m_rate_kb"]; $i++)
			{
				if($pricerank == $i)
				{
					$_strWeek .= "<option value=".$i." selected>".$datetype[$i]."</option>\r\n";
				}
				else
				{
					$_strWeek .= "<option value=".$i.">".$datetype[$i]."</option>\r\n";
				}	
			}
			$_strWeek .="</select></td>";
		}
	}
	$_strWeek .= "</tr>\r\n";

	$_strWeek .= "<tr>\r\n";
	for($i = 16; $i <= $_dayRow[$_Month]; $i++)
	{
		$_strWeek .= "<td align='center' valign='middle' bgcolor='#DDE3EC' class='text02'>".$i."</td>\r\n";
	}
	if($_dayRow[$_Month] != 31)
   	{
		if($_dayRow[$_Month] == 30)
		{
			
			$_strWeek .= "<td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td>\r\n";
		}
		elseif($_dayRow[$_Month] == 29)
		{
			$_strWeek .= "<td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td><td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td>\r\n";
		}
		else
		{
			  
				$_strWeek .= "<td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td><td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td>
				              <td width='25' align='center' valign='middle' bgcolor='#DDE3EC' class='text02 style5'>&nbsp;</td>\r\n";
		}
	}
	$_strWeek .= "</tr>\r\n";
	$_strWeek .= "<tr>\r\n";
	for($j=16;$j <=$_dayRow[$_Month];$j++)
	{
		$currentDateByWeek = date("w",strtotime($currentDateToyyyymm."-".$j));
		if($currentDateByWeek == 6)
		{
			$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02 style6'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
		}
		elseif($currentDateByWeek == 0)
		{
			$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02 style3'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
		}
		else
		{
			$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>".$_weekRow[$_language][$currentDateByWeek]."</td>\r\n";
		}
	}
	if($_dayRow[$_Month] != 31)
   	{
		if($_dayRow[$_Month] == 30)
		{
			$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
		}
		elseif($_dayRow[$_Month] == 29)
		{
			$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td><td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
		}
		else
		{
				$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td><td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>
				               <td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
		}
	}
	$_strWeek .= "</tr>\r\n";
	$_strWeek .= "<tr>\r\n";
    for($j=16;$j <=$_dayRow[$_Month];$j++)
	{
	
		$re_date = date("Ymd",strtotime($_Year."-".$_Month."-".$j));
		$pricerank = $calender->getPricerank($re_date);
		$_strWeek .= "<td height='40' align='center' valign='middle' bgcolor='#FFFFFF' class='text02'><select name='select".$j."' style='width:50px; '>\r\n";
		for($i=1;$i <= $baseArr["m_rate_kb"]; $i++)
		{
			if($pricerank == $i)
			{
				$_strWeek .= "<option value=".$i." selected>".$datetype[$i]."</option>\r\n";
			}
			else
			{
				$_strWeek .= "<option value=".$i.">".$datetype[$i]."</option>\r\n";
			}
		}
		$_strWeek .="</select></td>\r\n";	
    }
    if($_dayRow[$_Month] != 31)
   {
		if($_dayRow[$_Month] == 30)
		{ 
			$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
		}
	  	elseif($_dayRow[$_Month] == 29)
		{
		$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td><td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
		}
	else
	 { 
			$_strWeek .= "<td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td><td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>
			               <td align='center' valign='middle' bgcolor='#FFFFFF' class='text02'>&nbsp;</td>\r\n";
	 }
	}
	$_strWeek .= "<input type='hidden' name='MonthDay' value='".$_dayRow[$_Month]."'/><input type='hidden' name='Year' value='".$_Year."'/><input type='hidden' name='month' value='".$_Month."'/>";
	echo $_strYear.$_strWeek;
}
?>
